import { formatGMTDate, toPercent } from '../../../../assets/js/util.js'
import advance from '../advancedFilter.vue'
import echarts from 'echarts'
export default {
    data() {
        return {
            // 通用SRC
            SRC: '',
            // title
            title: "分职业留存",
            /* 的宽*/
            pageWidth: '100%',
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
            // 选择的时间
            date: '',
            // 渠道数组
            plats: [],
            // 渠道数组
            channels: [],
            // 服务器数组
            servers: [],
            postObj: {
                schoolDate: '',
                platform: '0',
                channel: '0',
                server: '0'
            },
            // 全服在线详细数据 表头
            tableHeader: [{
                prop: 'schoolName',
                label: '职业',
                width: '160px'
            }, {
                prop: 'newRole',
                label: '新增角色数',
                width: 'auto'
            }, {
                prop: 'the2Left',
                label: '次日留存',
                width: 'auto'
            }, {
                prop: 'the3Left',
                label: '3日留存',
                width: 'auto'
            }, {
                prop: 'the4Left',
                label: '4日留存',
                width: '180px'
            }, {
                prop: 'the5Left',
                label: '5日留存',
                width: 'auto'
            }, {
                prop: 'the5Left',
                label: '5日留存',
                width: 'auto'
            }, {
                prop: 'the6Left',
                label: '6日留存',
                width: 'auto'
            }, {
                prop: 'the7Left',
                label: '7日留存',
                width: 'auto'
            }, {
                prop: 'the14Left',
                label: '14日留存',
                width: 'auto'
            }, {
                prop: 'the30Left',
                label: '月留存',
                width: 'auto'
            }],
            tableData: [],
            tableDataAll: [],
            // 图表数据
            real: {
                tooltip: {
                    trigger: 'axis'
                },
                color: ['#2eb398', '#79bde9', '#9d79d8'],
                legend: {
                    top: '10px',
                    data: ['1', '2', '3']
                },
                grid: {
                    left: '1%',
                    right: '1%',
                    bottom: '15%',
                    containLabel: true
                },
                toolbox: {
                    right: '20',
                    feature: {
                        dataZoom: {
                            yAxisIndex: 'none'
                        },
                        magicType: {
                            type: ['line', 'bar']
                        },
                        restore: {},
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    data: [],
                    axisLabel: {
                        margin: 20,
                    },
                },
                yAxis: {
                    type: 'value',
                    axisLabel: {
                        show: true,
                        interval: 'auto',
                        formatter: function(val) {
                            val = toPercent(val);
                            return val
                        }
                    }
                },
                dataZoom: [{
                    handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
                    handleSize: '80%',
                    handleStyle: {
                        color: '#fff',
                        shadowBlur: 3,
                        shadowColor: 'rgba(0, 0, 0, 0.6)',
                        shadowOffsetX: 2,
                        shadowOffsetY: 2
                    }
                }],
                series: [{
                    name: '1',
                    type: 'line',
                    smooth: true,
                    data: [],
                    symbol: 'circle',
                }, {
                    name: '2',
                    type: 'line',
                    smooth: true,
                    data: [],
                    symbol: 'roundRect',
                }, {
                    name: '3',
                    type: 'line',
                    smooth: true,
                    data: [],
                    symbol: 'triangle',
                }, {
                    name: '4',
                    type: 'line',
                    smooth: true,
                    data: [],
                    symbol: 'triangle',
                }]
            },
        }
    },
    methods: {
        //初始化
        _init() {
            window.addEventListener('resize', function() {
                this.realTimeDataChart.resize()
            }.bind(this))
        },
        // 表格下载
        excel(tableIndex, sheet) {
            this.downloadExl(this.tableHeader, this.tableDataAll, sheet, sheet);
        },
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        // 时间改变 得到起始时间和结束时间
        dateChange(val) {
            if (val === undefined) {
                this.errorFun('请选择查看时间！')
            } else {
                this.postObj.schoolDate = val;
                this.getData(this.postObj);
            }
        },
        // 分页器
        // 控制每页的数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.tableControl();
        },
        // 页数
        handleCurrentChange(val) {
            this.currentPage = val;
            this.tableControl();
        },
        tableControl() {
            var data = this.tableDataAll;
            //转换为%比
            for (var i = 0; i < data.length; i++) {
                for (var j in data[i]) {
                    if (j === 'the2Left' || j === 'the3Left' || j === 'the4Left' || j === 'the5Left' || j === 'the6Left' || j === 'the7Left' || j === 'the14Left' || j === 'the30Left') {
                        data[i][j] = toPercent(data[i][j]);
                    }
                }
            }
            this.tableDataAll = data;
            this.tableData = this.tableDataAll.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
        },
        // 选择游戏的平台
        handleSelect(key, keyPath) {
            if (key === '4') {
                this.$refs.advance.filterShow = true;
                return
            }
            if (this.postObj['server'] === '0' && this.postObj['channel'] === '0') {
                this.postObj['server'] = '0';
                this.postObj['channel'] = '0';
                this.postObj['platform'] = key;
                this.getList(`updatePlatform.do?platform=${key}`);
                this.getData(this.postObj);
            } else {
                if (key === "3" || key === "2" || key === "9") {
                    this.errorFun("请修改为全渠道和全服！")
                }
            }
        },
        // 获取服务器list 平台list  服务器list
        getList(url) {
            var that = this;
            var pushData = function(pushArr, attr, data) {
                that[pushArr] = [];
                for (var i in data[attr]) {
                    that[pushArr].push({
                        value: i,
                        label: data[attr][i]
                    })
                }
            };
            this.$http.jsonp(`${this.SRC}player/alivePlayer/${url}`).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                if (parseInt(data["res"]) === 0) {
                    // 后台select的数据
                    var lists = ["platformItmes", "channelItmes", "serverItmes"];
                    // 页面select名字
                    var nameLists = ["plats", "channels", "servers"];
                    for (var i = 0; i < lists.length; i++) {
                        if (data[lists[i]] !== undefined) {
                            pushData(nameLists[i], lists[i], data);
                        }
                    }
                } else {
                    this.getErrorMsg(Number(data.res))
                }
            })
        },
        // 获取数据
        getData(data) {
            var that = this;
            var p = new Promise(function(resolve, reject) {
                that.dataTotal = 0;
                that.loading = true;
                that.tableData = [];
                that.tableDataAll = [];
                that.$http.jsonp(`${that.SRC}player/alivePlayer/search2.do`, {
                    params: data
                }).then(function(res) {
                    resolve(res.body)
                })
            })
            p.then(function(value) {
                that.loading = false;
                var data = JSON.parse(value)
                var d = JSON.parse(data.replace(/\"\{\"/g, '\{\"').replace(/\"\}\"/g, '\"\}'));
                if (parseInt(d["res"]) === 0) {
                    that.dataTotal = d.aliveRoleSchoolDatas.length;
                    that.tableDataAll = d.aliveRoleSchoolDatas;
                    that.tableControl();
                    that.echartsData(d);
                } else {
                    that.errorFun(data["msg"])
                }
            })
        },
        // 改变表数据
        echartsData(d) {
            this.realLoadShow = false;
            if (d.aliveRoleSchoolChart === undefined) {
                var data = {
                    "columnkeys": [],
                    "names": [],
                    "data": [
                        []
                    ]
                }
            } else {
                var data = d['aliveRoleSchoolChart']['jsonStr'];
            }
            // 实时数据获取到数据渲染
            this.realTimeDataChart.setOption({
                legend: {
                    data: data['names']
                },
                xAxis: {
                    data: data.columnkeys
                },
                series: [{
                        name: data.names[0],
                        data: data.data[0]
                    },
                    {
                        name: data.names[1],
                        data: data.data[1]
                    },
                    {
                        name: data.names[2],
                        data: data.data[2]
                    },
                    {
                        name: data.names[3],
                        data: data.data[3]
                    }
                ]
            });


        },
    },
    components: {
        'v-advance': advance
    },
    computed: {
        xlsx() {
            return this.title + '.xlsx'
        }
    },
    mounted() {
        // 初始化URL
        this.SRC = this.$store.getters.src2;
        this.date = new Date(new Date().getTime() - 86400000 * 2);
        this.postObj.schoolDate = formatGMTDate(new Date().getTime() - 86400000 * 2, true);
        // 获取到服务器 平台 渠道 list
        this.getList("initParam.do");
        // 实时图
        this.realTimeDataChart = echarts.init(document.getElementById('realTimeData'));
        this.realTimeDataChart.setOption(this.real);
        // 请求数据
        this.getData(this.postObj);

        this._init();
    }
}